package org.sys.transporttrainsys.service;

import org.sys.transporttrainsys.entity.Student;
import org.sys.transporttrainsys.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public List<Student> findStudents(String name, Long organizationId) {
        if (name != null && organizationId != null) {
            return studentRepository.findByNameAndOrganizationId(name, organizationId);
        } else if (name != null) {
            return studentRepository.findByName(name);
        } else if (organizationId != null) {
            return studentRepository.findByOrganizationId(organizationId);
        } else {
            return studentRepository.findAll();
        }
    }

    public Student saveStudent(Student student) {
        return studentRepository.save(student);
    }

    public Student updateStudent(Long id, Student student) {
        student.setId(id);
        return studentRepository.save(student);
    }

    public void deleteStudent(Long id) {
        studentRepository.deleteById(id);
    }
}